﻿$(document).ready(function () {
    var light = $("#J_light");
    var J_item = $("#J_form_activity");
    var J_form_hidden = $("#J_form_hidden");
    var J_form_mobile = $("#J_form_mobile");
    var J_form_mobilecheck = $("#J_form_mobilecheck");

    J_item.find("input:visible").blur(function () {
        var t = $(this);
        var index = $.inArray(this, $.makeArray($("#J_form_activity input:visible")));
        var reg = [/^(((13[0-9]{1})|(15[0-35-9]{1})|(18[025-9]{1}))+\d{8})$/, /^(((13[0-9]{1})|(15[0-35-9]{1})|(18[025-9]{1}))+\d{8})$/];
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        if (!reg[index].exec(val)) {
            alert("请填写正确手机号码");
            return false;
        };
        t.val(val);
        return true;
    });

    J_item.submit(function () {
        //表单输入验证
        if (J_form_hidden.val() == "1") {
            J_item.submit();
            return;
        };
        J_form_mobile_val = J_form_mobile.val().replace(/(^\s*)|(\s*$)/g, "");
        J_form_mobilecheck_val = J_form_mobilecheck.val().replace(/(^\s*)|(\s*$)/g, "");
        var reg = /^(((13[0-9]{1})|(15[0-35-9]{1})|(18[025-9]{1}))+\d{8})$/;
        if (!reg.exec(J_form_mobile_val)) {
            alert("请填写正确手机号码");
            return false;
        };
        if (!reg.exec(J_form_mobilecheck_val)) {
            alert("请填写正确确认手机号码");
            return false;
        };
        if (J_form_mobile_val != J_form_mobilecheck_val) {
            alert("请确认两次填写的号码是否相同");
            return false;
        };
        //exec
        var t = $(this);
        var url = J_item.attr("data-action");
        var data = {};
        data[J_form_mobile.attr("name")] = J_form_mobile.val();
        $.ajax({
            async: true,
            type: "post",
            url: url,
            data: data,
            dataType: "text",
            success: function (msg) {
                var data = eval("(" + msg + ")");
                var status = data["status"];
                if (status == "1") {
                    J_form_hidden.val("1");
                    J_item.submit();
                    return false;
                }
                light.show();
                light.find(".light-bg").css("height", $(document).height());
                light.find(".light-box").css({ left: (J_item.offset().left + 160), top: t.offset().top + 20 });
                light.find(".light-iframe").css("height", $(document).height());
                return false;
            },
            error: function () {
                alert("抱歉，出错了！请刷新页面后再试。");
                return false;
            }
        });
        return false;
    });
    light.find(".light-close").click(function () {
        light.hide();
    });
    //提示关闭
    $("#J_light_sign .light-close").click(function () {
        $("#J_light_sign").hide();
    });

    //login
    var J_form = $("#J_form");
    J_form.submit(function () {
        var t = $(this);
        var input = t.find("input");
        if (input.eq(0).val().length < 1) { alert("请填写用户名"); return false; };
        if (input.eq(1).val().length < 1) { alert("请填写密码"); return false; };
        return true;
    });


});

var clock = $("#J_clock");
var num = parseInt(clock.attr("data-time"));
function count() {
    num = num - 1;
    d = Math.floor(num / 86400);
    h = Math.floor((num - d*86400) / 3600);
    m = Math.floor((num - d * 86400 - h * 3600) / 60);
    dd = d > 0 ? "<span>" + d + "</span>" + "天" : "";
    hh = h > 0 ? "<span>" + h + "</span>" + "小时" : "";
    mm = m > 0 ? "<span>" + m + "</span>" + "分钟" : "";
    clock.html("活动倒计时:" + dd + hh + mm);
    if (num < 1) {
        clearInterval(countdown);
        clock.html("活动已结束");
    };
};
countdown = setInterval(count, 1000);